<template>
  <Dialog v-model="dialogVisible" title="指派网格用户">
    <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="120px">
      <el-form-item label="名称">
        <el-input v-model="formData.name" :disabled="true" />
      </el-form-item>
      <el-form-item label="公众监督员">
        <el-input v-model="formData.userName" :disabled="true" />
      </el-form-item>
      <el-form-item label="网格">
        <el-input v-model="formData.gridName" :disabled="true" />
      </el-form-item>
      <el-form-item label="网格员" prop="gridUserId">
        <el-select v-model="formData.gridUserId" placeholder="请选择网格员" clearable ref="gridUserIdRef" @change="handleGridUserIdChange">
          <el-option v-for="item in userList" :key="item.id" :label="item.nickname" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import { AqiApi, AqiVO } from '@/api/envir/aqi'
import * as UserApi from '@/api/system/user'

defineOptions({ name: 'SystemUserAssignRoleForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  id: -1,
  name: '', // 
  userName: '', // 公众监督员
  gridName: '', // 网格名称
  gridUserId: -1, // 网格用户ID
  gridUserName: '',
})
const formRules = reactive({
  gridUserId: [{ required: true, message: '网格用户不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref
const userList = ref([] as UserApi.UserVO[]) // 用户的列表

const handleGridUserIdChange = (value: number) => {
  const user = userList.value.find(item => item.id === value)
  if (user) {
    formData.value.gridUserName = user.nickname
  }
}

const gridUserIdRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (row: AqiVO) => {
  dialogVisible.value = true
  resetForm()
  // 设置数据
  formData.value.id = row.id
  formData.value.name = row.name
  formData.value.userName = row.userName
  formData.value.gridName = row.gridName
  // 获得用户列表
  userList.value = await UserApi.getNEPGUserList()

  if(row.gridUserId && row.gridUserName){
    formData.value.gridUserId = row.gridUserId
    formData.value.gridUserName = row.gridUserName
  }

}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    await AqiApi.assignGridUser({
      id: formData.value.id,
      gridUserId: formData.value.gridUserId,
      gridUserName: formData.value.gridUserName,
    })
    message.success(t('common.updateSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success', true)
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: -1,
    name: '', // 
    userName: '', // 公众监督员
    gridName: '', // 网格名称
    gridUserId: -1, // 网格用户ID
    gridUserName: '',
  }
  formRef.value?.resetFields()
  // nextTick(() => {
  //   gridUserIdRef.value?.clear()
  // })
}
</script>
